//
//  PAHomePageViewController.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-4.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PAHomePageViewController.h"
#import "PublicDefine.h"
#import "PAHomePageView.h"
#import "PABaseViewController.h"
#import "PAPersonalViewController.h"
#import "PARemitMoneyViewController.h"
#import "PADataCenter.h"
#import "PATimeLineInfo.h"
#import "PAAsynImageView.h"

@interface PAHomePageViewController ()
@property(nonatomic, strong)UILabel * homeNameLable;
@property(nonatomic, strong)UILabel * homeMoneyLable;
@property(nonatomic, strong)UIImageView * whiteSycleBgIV;
@end

@implementation PAHomePageViewController
@synthesize homeMoneyLable      = _homeMoneyLable;
@synthesize homeNameLable       = _homeNameLable;
@synthesize photoImage          = _photoImage;
@synthesize whiteSycleBgIV      = _whiteSycleBgIV;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self){
        PADataCenter *dataCenter = [PADataCenter sharedInstance];
        homePageView = [[PAHomePageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_PHONE, HEIGHT_CURRENT - 20)];
        homePageView.parentController = self;
        self.view = homePageView;
        
        UIView * navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100/2)];
        navView.backgroundColor = [UIColor clearColor];
        
        UIImageView * navImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_detail.png"]];
        navImage.frame = CGRectMake(0, 0, self.view.frame.size.width, 100/2);
        navImage.backgroundColor = [UIColor clearColor];
        [navView addSubview:navImage];
        
        self.homeNameLable = [[UILabel alloc] initWithFrame:CGRectMake(0, (98-80)/2-2, 120, 40)];
        self.homeNameLable.backgroundColor = [UIColor clearColor];
        self.homeNameLable.text = @"钱小婉";
        self.homeNameLable.textColor = [UIColor colorWithHexString:@"#ffffff"];
        self.homeNameLable.textAlignment = NSTextAlignmentCenter;
        [self.homeNameLable setFont:[UIFont boldSystemFontOfSize:20]];
        [navView addSubview:self.homeNameLable];
        
        
        self.whiteSycleBgIV = [[UIImageView alloc] initWithFrame:CGRectMake(120 +(80-88/2)/2-0.5, (98-88)/2-4, 88/2+4, 88/2+4)];
        self.whiteSycleBgIV.backgroundColor = [UIColor clearColor];
        self.whiteSycleBgIV.image = [UIImage imageNamed:@"home_myphoto.png"];
        [navView addSubview:self.whiteSycleBgIV];
        
        
//<<<<<<< .mine
        self.photoImage = [[PAAsynImageView alloc] initWithFrame:CGRectMake(120 + (80-88/2)/2+4, (98-88)/2-1, 88/2-5, 88/2-5)];
        self.photoImage.defaultImg = [UIImage imageNamed:@"home_myphoto_non.png"];
//=======
//        self.photoImage = [[PAAsynImageView alloc] initWithImage:[UIImage imageNamed:@"home_myphoto_non.png"]];
//        self.photoImage.frame = CGRectMake(120 + (80-88/2)/2+4, (98-88)/2-1, 88/2-4, 88/2-4);
//>>>>>>> .r6130
        [self.photoImage setImgUrl:dataCenter.selfInfo.baseInfo.headImageUrl key:dataCenter.selfInfo.baseInfo.phoneNum digest:dataCenter.selfInfo.baseInfo.headImageDigest];
        if (!dataCenter.selfInfo.baseInfo.headImageUrl) {
            self.whiteSycleBgIV.hidden = YES;
            self.photoImage.frame = CGRectMake(120 +(80-88/2)/2-2, (98-88)/2-4, 88/2+4, 88/2+4);
        }else{
            self.whiteSycleBgIV.hidden = NO;
            self.photoImage.frame = CGRectMake(120 + (80-88/2)/2+4, (98-88)/2-1, 88/2-5, 88/2-5);
        }
        self.photoImage.cornerRadius = self.photoImage.frame.size.height/2;
        self.photoImage.backgroundColor = [UIColor clearColor];
        [navView addSubview:self.photoImage];
        if (self.photoImage.imgUrl) {
        }
        

        UIButton * photoImageBT = [UIButton buttonWithType:UIButtonTypeCustom];
        photoImageBT.frame = CGRectMake(0, 0, 320, 88/2);
        photoImageBT.backgroundColor = [UIColor clearColor];
        [photoImageBT addTarget:self action:@selector(gotoPersonal:) forControlEvents:UIControlEventTouchUpInside];
        [navView addSubview:photoImageBT];
        
        UILabel *kBalanceLabel = [[UILabel alloc] initWithFrame:CGRectMake((320-120), (98-80)/2, 40, 40)];
        kBalanceLabel.backgroundColor = [UIColor clearColor];
        kBalanceLabel.text = @"余额:";
        kBalanceLabel.textColor = [UIColor whiteColor];
        kBalanceLabel.textAlignment = NSTextAlignmentRight;
        [kBalanceLabel setFont:[UIFont systemFontOfSize:14.0]];
        [navView addSubview:kBalanceLabel];

        self.homeMoneyLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(kBalanceLabel.frame), (98-80)/2, 68, 40)];
        self.homeMoneyLable.backgroundColor = [UIColor clearColor];
        self.homeMoneyLable.text = @"665.0";
        self.homeMoneyLable.textColor = [UIColor colorWithHexString:@"#ffad4c"];
        self.homeMoneyLable.textAlignment = NSTextAlignmentLeft;
        [self.homeMoneyLable setFont:[UIFont systemFontOfSize:20]];
        [navView addSubview:self.homeMoneyLable];
        
        UILabel *unitLAB = [[UILabel alloc] initWithFrame:CGRectMake(276, 9, 40, 40)];
        unitLAB.backgroundColor = [UIColor clearColor];
        unitLAB.text = @"元";
        unitLAB.textColor = [UIColor whiteColor];
        unitLAB.textAlignment = NSTextAlignmentRight;
        [unitLAB setFont:[UIFont systemFontOfSize:14.0]];
        [navView addSubview:unitLAB];
        [homePageView addSubview:navView];
        
        UIView * barView = [[UIView alloc] initWithFrame:CGRectMake(0, (iPhone5?548:460)-30, self.view.frame.size.width, 60/2)];
        barView.backgroundColor = [UIColor whiteColor];
        
        UIImageView * barImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_bottom.png"]];
        barImage.frame = CGRectMake(0, self.view.frame.size.height-30, self.view.frame.size.width, 30);
        barImage.backgroundColor = [UIColor clearColor];
        [barView addSubview:barImage];
        
        CGFloat x,y,w,h;
        x = 257/2;  y = (60-32)/2;  w = 32/2;  h = 32/2;
        UIButton * firstButon = [UIButton buttonWithType:UIButtonTypeCustom]; //前进button
        firstButon.backgroundColor = [UIColor clearColor];
        firstButon.frame = CGRectMake(x, y, w, h);
        [firstButon setImage:[UIImage imageNamed:@"home_time2.png"] forState:UIControlStateNormal];
        [barView addSubview:firstButon];
        
        x = (257+67)/2;  y = (60-36)/2 ; w = 27/2;  h = 36/2;
        UIButton * secondButon = [UIButton buttonWithType:UIButtonTypeCustom]; //地图button
        secondButon.backgroundColor = [UIColor clearColor];
        secondButon.frame = CGRectMake(x, y, w, h);
        [secondButon setImage:[UIImage imageNamed:@"home_locat2.png"] forState:UIControlStateNormal];
        [barView addSubview:secondButon];
        
        [homePageView addSubview:barView];
        barView.hidden = YES;
        
        PASelfInfo *selfInfo = [PADataCenter sharedInstance].selfInfo;
        
        self.homeMoneyLable.text = [NSString stringWithFormat:@"%.2f",selfInfo.availableBalance];
        self.homeNameLable.text = [[PADataCenter sharedInstance] selfInfo].baseInfo.realName;
        
        [selfInfo addObserver:self forKeyPath:K_availableBalance options:NSKeyValueObservingOptionNew context:nil];
        [selfInfo addObserver:self forKeyPath:@"timeLineInfos" options:NSKeyValueObservingOptionNew context:nil];
        [selfInfo addObserver:self forKeyPath:@"baseInfo" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)gotoPersonal:(id)sender
{
    [self startAnimition:self.view sendState:@"up"] ;
}

- (void)testNext:(id)sender
{
    int tag = [(UIButton *)sender tag];

    if (tag == 1)
    {
        [self startAnimition:self.view sendState:@"up"] ;
    }
    if (tag == 2 || tag == 3 || tag == 4)
    {
        if (!remitMoneyController)
        {
            remitMoneyController = [[PARemitMoneyViewController alloc] initWithNibName:nil bundle:nil];
        }
        [remitMoneyController sendMarkType:20];
        remitMoneyController.targetInfo = nil;
        [self.parentViewController.navigationController pushViewController:remitMoneyController animated:YES];
        [remitMoneyController showContact:nil];
    }
}

- (void)startAnimition:(UIView *)sender sendState:(NSString *)getState
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve: UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    
    CGRect homeViewRect = sender.frame;
    if ([getState isEqualToString:@"up"])
    {
        homeViewRect.origin.y = iPhone5?548:460;
    }
    else if([getState isEqualToString:@"down"])
    {
        homeViewRect.origin.y = 0;
    }
    
    sender.frame = homeViewRect;

    [UIView commitAnimations];
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)selectHistoryTradeTargetIndexpath:(NSIndexPath *)indexPath
{
    PASelfInfo *selfInfos = [PADataCenter sharedInstance].selfInfo;
    PATimeLineInfo *timeLineInfo = [selfInfos.timeLineInfos objectAtIndex:indexPath.row];
    
    if (!remitMoneyController)
    {
        remitMoneyController = [[PARemitMoneyViewController alloc] initWithNibName:nil bundle:nil];
    }
    remitMoneyController.targetInfo = timeLineInfo.targetInfo;
    [remitMoneyController sendMarkType:10];
    [self.parentViewController.navigationController pushViewController:remitMoneyController animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    PASelfInfo *selfInfo = [[PADataCenter sharedInstance] selfInfo];
    [homePageView refreshTradeHistory:selfInfo.timeLineInfos];
}

#pragma mark - KVO 监听 数据变化
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    PASelfInfo *selfInfo = [PADataCenter sharedInstance].selfInfo;
    if ([keyPath isEqualToString:K_availableBalance]) {
        self.homeMoneyLable.text = [NSString stringWithFormat:@"%.2f",selfInfo.availableBalance];
    }
    else if([keyPath isEqualToString:@"timeLineInfos"]){
        [homePageView refreshTradeHistory:selfInfo.timeLineInfos];
    }else if ([keyPath isEqualToString:@"baseInfo"]){
        NSLog(@"homePage baseInfo ");
        self.homeNameLable.text = [[PADataCenter sharedInstance] selfInfo].baseInfo.realName;
        
        if (!selfInfo.baseInfo.headImageUrl) {
            self.whiteSycleBgIV.hidden = YES;
            self.photoImage.frame = CGRectMake(120 +(80-88/2)/2-2, (98-88)/2-4, 88/2+4, 88/2+4);
        }else{
            self.whiteSycleBgIV.hidden = NO;
            self.photoImage.frame = CGRectMake(120 + (80-88/2)/2+4, (98-88)/2-1, 88/2-5, 88/2-5);
        }
        [self.photoImage setImgUrl:selfInfo.baseInfo.headImageUrl key:selfInfo.baseInfo.phoneNum digest:selfInfo.baseInfo.headImageDigest];
        self.photoImage.cornerRadius = self.photoImage.frame.size.height/2;
        
    }else{
        /*
         * do nothing
         */
    }
}
@end
